//
//  KLRefreshTableHeaderView.m
//  keylime-iphone
//
//  Created by Jesse Curry on 7/17/10.
//  Copyright 2010 Circonda, Inc. All rights reserved.
//

#import "KLRefreshTableHeaderView.h"
#import <QuartzCore/QuartzCore.h>

#define TEXT_COLOR [UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha: 1.0]
#define BORDER_COLOR [UIColor colorWithRed: 0.1 green: 0.1 blue: 0.1 alpha: 1.0]

////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation KLRefreshTableHeaderView
@synthesize isFlipped;
@synthesize lastUpdatedDate;

- (id)initWithFrame: (CGRect)frame
{
	if ( self = [super initWithFrame: frame] )
	{
		self.backgroundColor = [UIColor colorWithRed: 25 / 255.0
											   green: 25 / 255.0 
												blue: 25 / 255.0 
											   alpha: 1.0];

		lastUpdatedLabel = [[UILabel alloc] initWithFrame: CGRectMake(0.0f, 
																	  frame.size.height - 30.0f,
																	  320.0f, 
																	  20.0f)];
		lastUpdatedLabel.font = [UIFont systemFontOfSize: 12.0f];
		lastUpdatedLabel.textColor = TEXT_COLOR;
		lastUpdatedLabel.shadowColor = [UIColor colorWithWhite: 0.9f alpha: 1.0f];
		lastUpdatedLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
		lastUpdatedLabel.backgroundColor = self.backgroundColor;
		lastUpdatedLabel.opaque = YES;
		lastUpdatedLabel.textAlignment = UITextAlignmentCenter;
		[self addSubview: lastUpdatedLabel];
		[lastUpdatedLabel release];

		statusLabel = [[UILabel alloc] initWithFrame: CGRectMake(0.0f,
																 frame.size.height - 48.0f, 
																 320.0f, 
																 20.0f)];
		statusLabel.font = [UIFont boldSystemFontOfSize: 13.0f];
		statusLabel.textColor = TEXT_COLOR;
		statusLabel.shadowColor = [UIColor colorWithWhite: 0.9f alpha: 1.0f];
		statusLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
		statusLabel.backgroundColor = self.backgroundColor;
		statusLabel.opaque = YES;
		statusLabel.textAlignment = UITextAlignmentCenter;
		[self setStatus: ViewStatusPullToReload];
		[self addSubview: statusLabel];
		[statusLabel release];

		arrowImage = [[UIImageView alloc] initWithFrame:
		              CGRectMake(25.0f, frame.size.height - 65.0f, 30.0f, 55.0f)];
		arrowImage.contentMode = UIViewContentModeScaleAspectFit;
		arrowImage.image = [UIImage imageNamed: @"refreshArrow.png"];
		[arrowImage layer].transform = CATransform3DMakeRotation(M_PI, 0.0f, 0.0f, 1.0f);
		[self addSubview: arrowImage];
		[arrowImage release];

		activityView = [[UIActivityIndicatorView alloc] 
						initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
		activityView.frame = CGRectMake(25.0f, frame.size.height - 38.0f, 20.0f, 20.0f);
		activityView.hidesWhenStopped = YES;
		[self addSubview: activityView];
		[activityView release];

		isFlipped = NO;
	}
	return self;
}

- (void)dealloc
{
	activityView = nil;
	statusLabel = nil;
	arrowImage = nil;
	lastUpdatedLabel = nil;
	
	[super dealloc];
}

#pragma mark -
- (void)drawRect: (CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextDrawPath(context,  kCGPathFillStroke);
	[BORDER_COLOR setStroke];
	CGContextBeginPath(context);
	CGContextMoveToPoint(context, 0.0f, self.bounds.size.height - 1);
	CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height - 1);
	CGContextStrokePath(context);
}

- (void)flipImageAnimated: (BOOL)animated
{
	[UIView beginAnimations: nil context: NULL];
	[UIView setAnimationDuration: animated ? 0.18 : 0.0];
	[arrowImage layer].transform = isFlipped 
	? CATransform3DMakeRotation(M_PI, 0.0f, 0.0f, 1.0f) 
	: CATransform3DMakeRotation(M_PI * 2, 0.0f, 0.0f, 1.0f);
	[UIView commitAnimations];

	isFlipped = !isFlipped;
}

- (void)setLastUpdatedDate: (NSDate*)newDate
{
	if ( newDate )
	{
		if ( lastUpdatedDate != newDate )
		{
			[lastUpdatedDate release];
		}

		lastUpdatedDate = [newDate retain];

		NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
		[formatter setDateStyle: NSDateFormatterShortStyle];
		[formatter setTimeStyle: NSDateFormatterShortStyle];
		lastUpdatedLabel.text = [NSString stringWithFormat:
		                         @"Last Updated: %@", [formatter stringFromDate: lastUpdatedDate]];
		[formatter release];
	}
	else
	{
		lastUpdatedDate = nil;
		lastUpdatedLabel.text = @"Last Updated: Never";
	}
}

- (void)setStatus: (ViewStatus)status
{
	switch ( status )
	{
	case ViewStatusReleaseToReload:
		statusLabel.text = @"Release to refresh...";
		break;
	case ViewStatusPullToReload:
		statusLabel.text = @"Pull down to refresh...";
		break;
	case ViewStatusLoading:
		statusLabel.text = @"Loading...";
		break;
	default:
		break;
	}
}

- (void)toggleActivityView: (BOOL)isOn
{
	if ( isOn )
	{
		[activityView startAnimating];
		arrowImage.hidden = YES;
		[self setStatus: ViewStatusLoading];
	}
	else
	{
		[activityView stopAnimating];
		arrowImage.hidden = NO;
	}
}

@end
